package vn.com.pavibook.admin.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vn.com.pavibook.dao.AccountDao;
import vn.com.pavibook.dao.AccountTypeDao;
import vn.com.pavibook.dao.BookDao;
import vn.com.pavibook.dao.PublisherDao;
import vn.com.pavibook.domain.model.Account;
import vn.com.pavibook.domain.model.Book;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.Publisher;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.PublisherVO;
import vn.com.pavibook.utils.DataHelper;
import vn.com.pavibook.utils.GenerateMD5;

@Service
public class ManagePublisherService {

	private static final Logger logger = LoggerFactory
			.getLogger(ManagePublisherService.class);

	@Autowired
	private PublisherDao publisherDao;

	@Autowired
	private AccountTypeDao accountTypeDao;

	@Autowired
	private AccountDao accountDao;

	@Autowired
	private BookDao bookDao;
	
	public boolean addNewPublisher(PublisherVO publisherVO) {
		logger.info("addNewPublisher()");
		int accountId = insertNewAccount(publisherVO);
		publisherVO.setAccountId(accountId);
		Publisher publisher = convertToPublisher(publisherVO);
		try {
			publisherDao.addNewPublisher(publisher);
		} catch (Exception e) {
			logger.info("Error save publisher: " + e.getMessage());
			return false;
		}
		return true;
	}

	public void updatePublisherInfo(PublisherVO publisherVO) {
		logger.info("updatePublisherInfo()");
		Publisher publisher = convertToPublisher(publisherVO);
		publisherDao.updatePublisherInfo(publisher);
	}

	public boolean checkOldPass(String oldPass, Integer id) {
		Account oldAcc = accountDao.getAccountById(id);
		String oldPassword = oldAcc.getPassword();
		oldPass = GenerateMD5.MD5(oldPass);
		if (oldPassword.equals(oldPass)) {
			return true;
		}
		return false;
	}
	public boolean checkExistedUsername(String username){
		return publisherDao.isExistedUsername(username);
	}
	public Publisher convertToPublisher(PublisherVO publisherVO) {
		Publisher publisher = new Publisher();
		publisher.setActive(true);
		publisher.setBossName(StringUtils.EMPTY);
		publisher.setAddress(publisherVO.getAddress());
		publisher.setAccountId(publisherVO.getAccountId());
		publisher.setDescription(publisherVO.getDescription());
		publisher.setEmailBussiness(publisherVO.getEmailBussiness());
		publisher.setName(publisherVO.getName());
		publisher.setPhone(publisherVO.getPhone());
		publisher.setRegisterDate(new Date());
		publisher.setTitle(StringUtils.EMPTY);
		if (StringUtils.isNotEmpty(String.valueOf(publisherVO.getId()))) {
			publisher.setId(publisherVO.getId());
		}
		return publisher;
	}

	public int insertNewAccount(PublisherVO publisherVO) {
		int idRolePublisher = accountTypeDao
				.getAccountTypeByRole(DataHelper.UserRole.ROLE_PUBLISHER
						.getCode());
		Account account = new Account();
		account.setActive(true);
		account.setEmail(publisherVO.getEmailBussiness());
		account.setPassword(GenerateMD5.MD5(publisherVO.getPassword()));
		account.setTypeAccountId(idRolePublisher);
		account.setUsername(publisherVO.getUsername());
		return accountDao.insertNewAccount(account);
	}

	public SearchResult<Publisher> getPublisher(PagingInfo pagingInfo,
			String keyword) {
		return publisherDao.getPublisher(pagingInfo, keyword);
	}
	
	public void deletePublisherById(int id){
		try {
			List<Publisher> publishers = publisherDao.getPersonalInfoById(id).getSearchResult();
			if(publishers.size() != 0){
				int account_id = publishers.get(0).getAccountId();
				accountDao.deleteAccount(account_id);
			}
			List<Book> books = bookDao.getListBookByPublisher(id);
			if(books.size() != 0){
				for(Book book : books){
					bookDao.deleteBook(book.getId());
				}
			}
			publisherDao.deletePublisherById(id);
			
		} catch (Exception e) {
			logger.info("deletePublisherById : " + e.getMessage());
		}
	}
}
